﻿Ext.define('app.prm.document.ShareDoc',
{
    extend: 'Ext.window.Window',
    layout: 'border',
    modal: true,
    title: '文档分享',
    width: 800,
    height: 600,
    bodyStyle: 'padding:2px',
    docId: 0,
    projectId: 0,
    initComponent: function () {
        var me = this;
        me.items = [me.getSharePanel(), me.getShareHistory()];
        me.callParent();
    },
    onSave: function () {
        var me = this;
        var memberTree = me.getTaskMember();
        var checkeds = memberTree.getChecked();
        var members = [];
        var memberIds = [];
        for (var i = 0; i < checkeds.length; i++) {
            var node = checkeds[i];
            if (parseInt(node.data.id) > 0) {
                memberIds.push(node.data.id);
                members.push(node.data.text);
            }
        }
        var form = me.getSharePanel().getForm();
        if (!form.isValid()) {
            return;
        }
        if (memberIds.length == 0) {
            return;
        }
        var json = form.getValues();
        json.members = memberIds;
        json.DocId = me.docId;
        json.ProjectId = me.projectId;
        App.AjaxPost('/api/services/app/document/AddDocShare', json, function () {
            Ext.Msg.alert("系统提示", "添加成功");
            form.reset();
            memberTree.reset();
            me.getShareHistory().store.reload();
        });
        //me.fireEvent("savesuccess", memberIds, members.toString(), quans.toString(), quanIds);

    },
    getSharePanel: function () {
        var me = this;
        if (!this.sharePanel) {
            this.sharePanel = new Ext.form.Panel({
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                border: false,
                region: 'north',
                height: 200,
                layout: 'fit',
                items: [
                    {
                        layout: 'border',
                        border: false,
                        items: [
                           me.getTaskMember(),
                           {

                               region: 'center',
                               border: false,
                               fieldDefaults: {
                                   labelWidth: 90,
                                   labelAlign: "right"
                               },
                               items: [
                               {
                                   xtype: 'datefield',
                                   format: 'Y-m-d',
                                   value: Ext.Date.add(new Date(), Ext.Date.DAY, 7),
                                   allowBlank: false,
                                   name: 'planFinshTime',
                                   fieldLabel: '截止时间',
                               },
                               {
                                   xtype: 'textarea',
                                   height: 120,
                                   anchor: '90%',
                                   width: 420,
                                   allowBlank: false,
                                   fieldLabel: '描述',
                                   name: 'Remark'
                               }
                               ]
                           }
                        ]
                    }
                ], buttons: [
                     {
                         text: '保存', icon: '/Content/images/page_save.png', handler: function () {
                             me.onSave();
                         }
                     }
                ]
            });
        }
        return me.sharePanel;
    },
    getTaskMember: function () {
        var me = this;
        if (!this.taskMember) {
            this.taskMember = new Ext.tree.Panel({
                border: true,
                region: 'west',
                width: 200,
                fieldLabel: '分享给',
                useArrows: false,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: false,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/Project/GetRoleUserTree?projectId=' + me.projectId
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) {
                        tree.expandAll();
                    },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    }
                }, reset: function () {
                    var nodes = this.getChecked();
                    if (nodes && nodes.length) {
                        for (var i = 0; i < nodes.length; i++) {
                            nodes[i].set('checked', false);
                        }
                    }
                }
            });
        }
        return this.taskMember;
    },
    checkChild: function (node, check) {
        var me = this;
        node.set('checked', check);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkChild(child, check);
            });
        }
    },
    nodep: function (node) {
        var bnode = false;
        Ext.Array.each(node.childNodes, function (v) {
            if (v.data.checked) {
                bnode = true;
                return;
            }
        });
        return bnode;
    },
    checkParent: function (node) {
        var me = this;
        if (node.parentNode != null) {
            if (me.nodep(node.parentNode)) {
                node.parentNode.set('checked', true);
            } else {
                node.parentNode.set('checked', false);
            }
            me.checkParent(node.parentNode);
        }
    },
    getShareHistory: function () {
        var me = this;
        if (!this.shareHistory) {
            this.shareHistory = new Ext.grid.Panel({
                title: '分享历史',
                region: 'center',
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/document/GetDocumentShare?docId=' + me.docId, autoLoad: true }),
                columns: [
                    {
                        header: "创建时间", dataIndex: 'createOn', renderer: function (value, p, record) {
                            return Ext.util.Format.date(value, 'Y-m-d');
                        }
                    },
                    {
                        header: "创建人", dataIndex: 'createBy'
                    },
                     {
                         header: "分享的对象", dataIndex: 'shareTo'
                     },
                      {
                          header: "截止时间", dataIndex: 'finishDate', renderer: function (value, p, record) {
                              return Ext.util.Format.date(value, 'Y-m-d');
                          }
                      },
                        {
                            header: '描述', width: 80, hidden: true, maxWidth: 200, dataIndex: 'remark', renderer: function (value, p, record) {
                                p.tdAttr = 'data-qtip="' + value + '"';
                                return value;
                            }
                        },
                    {
                        xtype: 'actioncolumn',
                        minWidth: 120,
                        header: '操作',
                        items: [
                        {
                            iconCls: 'edit',
                            tooltip: '收回',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                App.AjaxPost('/api/services/app/document/EndDocShare?shareId=' + record.get('shareId'), null, function () {
                                    grid.store.reload();
                                });
                            },
                            getClass: function (v, meta, record) {
                                if (new Date(record.data.finishDate) >= new Date()) {
                                    return 'doorout';
                                } else {
                                    return 'x-hidden';
                                }
                            }
                        }
                        ]
                    }
                ]
            });
        }
        return this.shareHistory;
    }
});